﻿Imports DTO
Imports BUS
Imports DevExpress.XtraEditors

Public Class frmThemNhaCungCap

    Public kt As Integer = 0

    Public maNCC As String
    Public tenNCC As String
    Public dc As String
    Public dt As String
    Public fax As String
    Public email As String
    Public nh As String
    Public mst As String
    Public stk As String
    Public maTT As Integer


    Private Sub cbxThemKhuVuc_Properties_ButtonClick(sender As Object, e As DevExpress.XtraEditors.Controls.ButtonPressedEventArgs) Handles cbxThemKhuVuc.Properties.ButtonClick
        If e.Button.Tag.ToString() = "cbxbtnThemKV" Then
            Dim frm As New frmThemKhuVuc
            frm.ShowDialog()
        End If
    End Sub

    Private Sub LoadCbxKhuVuc()
        cbxThemKhuVuc.Properties.DataSource = KhuVucBUS.LayDSKhuVucCbx()
        cbxThemKhuVuc.Properties.ValueMember = "MaKhuVuc"
        cbxThemKhuVuc.Properties.DisplayMember = "TenKhuVuc"
        SetEditValueByIndex(cbxThemKhuVuc, 0)
    End Sub

    'hàm có chức năng selectedindex = 0
    Private Sub SetEditValueByIndex(ByVal edit As GridLookUpEdit, ByVal index As Integer)
        Dim keyValue As Object = cbxThemKhuVuc.Properties.GetKeyValue(index)
        cbxThemKhuVuc.EditValue = keyValue
    End Sub

    Private Sub frmThemNhaCungCap_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        LoadCbxKhuVuc()
        If kt = 1 Then
            txtThemMaNCC.Text = maNCC
            txtThemTenNCC.Text = tenNCC
            txtThemDiaChiNCC.Text = dc
            txtThemDienThoaiNCC.Text = dt
            txtThemEmailNCC.Text = email
            txtThemFaxNCC.Text = fax
            txtThemNganHangNCC.Text = nh
            txtThemMSTNCC.Text = mst
            txtThemTKNCC.Text = stk
            If maTT = 1 Then
                chkConQuanLy.Checked = True
            Else
                chkConQuanLy.Checked = False
            End If
        End If
    End Sub

    Sub ThemKhuVuc()

        Dim ncc As New NhaCungCapDTO

        ncc.TenNhaCungCap = txtThemTenNCC.Text.Trim()
        ncc.ChiecKhau = Convert.ToDouble(cbcThemChiecKhauNCC.Text.Trim())
        ncc.ChucVu = txtThemChucVuNCC.Text.Trim()
        ncc.DiaChi = txtThemDiaChiNCC.Text.Trim()
        ncc.DiDong = txtThemMobileNCC.Text.Trim()
        ncc.Email = txtThemEmailNCC.Text.Trim()
        ncc.TrangWEB = txtThemWebsiteNCC.Text.Trim()
        ncc.TaiKhoan = txtThemTKNCC.Text.Trim()
        ncc.NguoiLienHe = txtThemNguoiLienHeNCC.Text.Trim()
        ncc.NganHang = txtThemNganHangNCC.Text.Trim()
        ncc.MaSoThue = txtThemMSTNCC.Text.Trim()
        ncc.GioiHanNo = Convert.ToDouble(cbcThemGiaHanNoNCC.Text.Trim())
        ncc.MaKhuVuc = cbxThemKhuVuc.EditValue.ToString()
        If chkConQuanLy.Checked = True Then
            ncc.MaTrangThai = 1
        Else
            ncc.MaTrangThai = 0
        End If
        ncc.Fax = txtThemFaxNCC.Text.Trim()
        ncc.DienThoai = txtThemDienThoaiNCC.Text.Trim()

        If NhaCungCapBUS.ThemNhaCungCap(ncc) Then
            DevExpress.XtraEditors.XtraMessageBox.Show("Thêm thành công nhà cung cấp", "Thông Tin", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Else
            DevExpress.XtraEditors.XtraMessageBox.Show("Tên nhà cung cấp không được để trống", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End If
    End Sub

    Sub CapNhatKhuVuc()
        Dim ncc As New NhaCungCapDTO
        ncc.MaNhaCungCap = txtThemMaNCC.Text.Trim()
        ncc.TenNhaCungCap = txtThemTenNCC.Text.Trim()
        ncc.ChiecKhau = Convert.ToDouble(cbcThemChiecKhauNCC.Text.Trim())
        ncc.ChucVu = txtThemChucVuNCC.Text.Trim()
        ncc.DiaChi = txtThemDiaChiNCC.Text.Trim()
        ncc.DiDong = txtThemMobileNCC.Text.Trim()
        ncc.Email = txtThemEmailNCC.Text.Trim()
        ncc.TrangWEB = txtThemWebsiteNCC.Text.Trim()
        ncc.TaiKhoan = txtThemTKNCC.Text.Trim()
        ncc.NguoiLienHe = txtThemNguoiLienHeNCC.Text.Trim()
        ncc.NganHang = txtThemNganHangNCC.Text.Trim()
        ncc.MaSoThue = txtThemMSTNCC.Text.Trim()
        ncc.GioiHanNo = Convert.ToDouble(cbcThemGiaHanNoNCC.Text.Trim())
        ncc.MaKhuVuc = cbxThemKhuVuc.EditValue.ToString()
        If chkConQuanLy.Checked = True Then
            ncc.MaTrangThai = 1
        Else
            ncc.MaTrangThai = 0
        End If
        ncc.Fax = txtThemFaxNCC.Text.Trim()
        ncc.DienThoai = txtThemDienThoaiNCC.Text.Trim()

        If NhaCungCapBUS.CapNhatNhaCungCap(ncc) Then
            DevExpress.XtraEditors.XtraMessageBox.Show("Cập nhật thành công nhà cung cấp", "Thông Tin", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Else
            DevExpress.XtraEditors.XtraMessageBox.Show("Tên nhà cung cấp không được để trống", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End If
    End Sub

    Private Sub frmThemKH_btnDongNCC_Click(sender As Object, e As EventArgs) Handles frmThemKH_btnDongNCC.Click
        Me.Close()
    End Sub

    Private Sub frmThemKH_btnThemNCC_Click(sender As Object, e As EventArgs) Handles frmThemKH_btnThemNCC.Click
        If kt = 1 Then
            CapNhatKhuVuc()
        Else
            ThemKhuVuc()
        End If
    End Sub
End Class